/**
 * Filename: DetailAdapter.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/15 10:08
 */
package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.SelectedImg;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

public class AlbumManageRecyViewAdapter extends
        RecyclerView.Adapter<AlbumManageRecyViewAdapter.ViewHolder> {
    private Context context;
    private ImageLoader mImageLoader;
    private LayoutInflater mInflater;
    private List<String> mDatas;

    public AlbumManageRecyViewAdapter(Context context, List<String> datats) {
        this.context = context;
        mImageLoader = ImageLoader.getInstance();
        mInflater = LayoutInflater.from(context);
        mDatas = datats;
    }

    public interface OnItemClickLitener {
        void onItemClick(View view, int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View arg0) {
            super(arg0);
        }

        SimpleDraweeView mImg;
        ImageView clear;
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    /**
     * 创建ViewHolder
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = mInflater.inflate(R.layout.yn_album_mamage_recy_detail,
                viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(view);
        viewHolder.mImg = (SimpleDraweeView) view
                .findViewById(R.id.id_dateil_item_image);
        viewHolder.clear = (ImageView) view
                .findViewById(R.id.id_dateil_item_clear);
        return viewHolder;
    }

    /**
     * 设置值
     */
    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int i) {
        final String path = mDatas.get(i);
        if (path.startsWith("http")) {
            ImageOptHelper.showFrescoNative(Uri.parse(path), viewHolder.mImg, context);
        } else {
            ImageOptHelper.showFrescoNative(Uri.parse("file://" + path), viewHolder.mImg, context);
        }
        viewHolder.clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mDatas.remove(i);
                notifyDataSetChanged();
                if (SelectedImg.mSelectedImage.contains(path)) {
                    SelectedImg.mSelectedImage.remove(path);
                }
                Intent selectIntent = new Intent(ACTIONs.actionUpdateReCyView);
                selectIntent.putExtra("url", path);
                context.sendBroadcast(selectIntent);
            }
        });
        if (mOnItemClickLitener != null) {
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickLitener.onItemClick(viewHolder.itemView, i);
                }
            });
        }
        viewHolder.setIsRecyclable(false);
    }

}
